package it.davial.mgwt.server;

import static it.davial.mgwt.server.Sql.Utility.SELECT_ID;
import static it.softphone.commons.util.LogUtil.logger; 
import it.davial.mgwt.client.ManagerId;


import java.text.ParseException;
import java.text.SimpleDateFormat;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Utility {

	public class Costants{
		public static final int    ENABLED  = 0;
		public static final int    DISABLED = 1;
		public static final String CONF_DB  = "derm_configuration";
	}
	
	public static class Jpa{

		
		private static EntityManagerFactory emf;
			
		
		public static EntityManagerFactory getEntityFactory(){
			if (emf==null) emf =Persistence.createEntityManagerFactory(Costants.CONF_DB);
			
			return emf;
		}
		
	}
	/**
	 * 
	 * @author vincenzotandurella
	 *
	 */
	public static class ManagmentId{
	
		public synchronized static int getNextId(IdEntity entity)throws Exception{
			EntityManager newEm=null;
			EntityTransaction tx =null;
			int resultId=0;
			Utility.Logs.startFunction("Utility", "getNextId", new String[]{entity.toString()});
			try{
				logger.info(String.format("try to create id : [%s]", entity));
				EntityManagerFactory emf=JpaUtility.getEntityFactory();
				if (emf==null){
					logger.warn("EntityManagerFactory is null...");
					throw new Exception();
				}
				newEm =emf.createEntityManager();
			    tx = newEm.getTransaction();
			    tx.begin();
			    ManagerId managerIdBean =(ManagerId)newEm.createNativeQuery(SELECT_ID,ManagerId.class).getSingleResult();
			   
			    logger.info(managerIdBean);
			    
			    if (entity.equals(IdEntity.AREA))   {resultId=managerIdBean.getIdArea()   +1;managerIdBean.setIdArea(resultId);}
			    if (entity.equals(IdEntity.FIGURE)) {resultId=managerIdBean.getIdFigure() +1;managerIdBean.setIdFigure(resultId);}
			    if (entity.equals(IdEntity.PATIENT)){resultId=managerIdBean.getIdPatient()+1;managerIdBean.setIdPatient(resultId);}
			    if (entity.equals(IdEntity.PHOTO))  {resultId=managerIdBean.getIdPhoto()  +1;managerIdBean.setIdPhoto(resultId);}
			    if (entity.equals(IdEntity.VISIT))  {resultId=managerIdBean.getIdVisit()  +1;managerIdBean.setIdVisit(resultId);}
			    logger.info(managerIdBean);
			    
			    newEm.merge(managerIdBean);
			    logger.info("merge ManagerId....");
			    tx.commit();
			   
			}catch(Exception e){
				logger.error("ManagmentId:getNextId",e);
				tx.rollback();
				throw e;
			}finally{
				newEm.close();
			}
			return resultId;
		}
	}
	
	public static class Date{
		private static final String FORMAT_WEB ="dd/MM/yyyy";
		private static final String FORMAT_DB  ="yyyyMMdd";
		private static final String DEFAULT_WEB_DATE="01/01/1900";
		private static final String DEFAULT_DB_DATE="19000101";
		
		private static final SimpleDateFormat SD_FORMAT_DB = new SimpleDateFormat(FORMAT_DB);
		private static final SimpleDateFormat SD_FORMAT_WEB = new SimpleDateFormat(FORMAT_WEB);
		
		public static  String dateDbToWebFormat(String date) throws ParseException{
			try{
				java.util.Date value=SD_FORMAT_DB.parse(date);
				return SD_FORMAT_WEB.format(value);
			}catch(Exception e){
				return DEFAULT_WEB_DATE;
			}
		}

		public static  String webFormatToDateDb(String date) throws ParseException{
			try{
				java.util.Date value=SD_FORMAT_WEB.parse(date);
				return SD_FORMAT_DB.format(value);
			}catch(Exception e){
				return DEFAULT_DB_DATE;
			}
		}
		
		public static  String nowDateDb() throws ParseException{
			try{
				java.util.Date value=new java.util.Date();
				return SD_FORMAT_DB.format(value);
			}catch(Exception e){
				return DEFAULT_DB_DATE;
			}
		}
	}
	public static class StringUtils{
		public static  String nullUtility(String value) throws ParseException{
			try{
				value=(value!=null && value.isEmpty())?null:value;
			}catch(Exception e){
				return null;
			}
			return value;
		}
		
	}
	
	public static class Logs{
		public static  void startFunction(String nameUtility,String nameFunction,String...args){
			logger.info("===================");
			logger.info("Utility :".concat(nameUtility));
			logger.info("Function :".concat(nameFunction));
			logger.info("args....");
			for (String param:args){
				logger.info(param);
			}
		}
	
		public static void startSQL(String sql,String...args){
			logger.info("===================");
			logger.info("Utility :".concat(sql));
			logger.info("args....");
			for (String param:args){
				logger.info(param);
			}
		}
	}
}
